﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace LosConn.Web.Pay
{
    public partial class PayListDetail : BasePage.AdminBasePage
    {
        public Model.Los_Guest modelGuest = new Model.Los_Guest();
        public Model.Los_Guest modelCheck = new Model.Los_Guest();
        public Model.Los_Guest modelEyeCheck = new Model.Los_Guest();
        public Model.Los_Guest modelRetinaCheck = new Model.Los_Guest();
        public DataTable dtRetail = null;
        public String Order_Money = String.Empty;//总金额
        public String html = String.Empty;
        public String orderDetailHtml = String.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                modelGuest.Guest_ID = Convert.ToInt32(Request["id"]);
                BLL.Los_Guest bllGuest = new BLL.Los_Guest();
                BLL.Los_Retail bllRetail = new BLL.Los_Retail();
                modelGuest = bllGuest.GetGuestModel(modelGuest);
                dtRetail = bllRetail.GetList(String.Format(" Retail_ID={0} ", modelGuest.Guest_RetailID)).Tables[0];
                //if (!String.IsNullOrEmpty(modelGuest.Guest_Birthday.ToString()))
                //{
                //    modelGuest.Guest_Birthday = Convert.ToDateTime(Convert.ToDateTime(modelGuest.Guest_Birthday).ToString("yyyy-MM-dd"));
                //}
                modelCheck = bllGuest.GetCheckModel(modelGuest);
                modelEyeCheck = bllGuest.GetEyeCheckModel(modelGuest);
                modelRetinaCheck = bllGuest.GetRetinaCheckModel(modelGuest);


                BLL.Los_Question bllQue = new BLL.Los_Question();
                DataTable dt = bllQue.GetQueAnsList(String.Format(" QueAns_GuestID={0} and QueAns_Status=100 ", modelGuest.Guest_ID)).Tables[0];
                StringBuilder sb = new StringBuilder(1000);
                Int32 idx = 0;
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        idx++;
                        sb.Append("<p class=\"amount-word\">");
                        sb.AppendFormat("{0}、{1}： <span>{2}</span>", idx, dr["QueAns_QuestionName"], dr["QueAns_AnswerName"]);
                        sb.Append("</p>");
                    }
                }
                html = sb.ToString();

                GetOrderDetailHtml(modelGuest.Guest_ID);
            }
        }

        /// <summary>
        /// 获取订单详细列表
        /// </summary>
        /// <param name="Guest_ID"></param>
        private void GetOrderDetailHtml(Int32 Guest_ID)
        {
            BLL.Los_Order bll = new BLL.Los_Order();
            StringBuilder sb = new StringBuilder(5000);
            DataTable dt = bll.GetDetailList(String.Format("Order_GuestID={0}", Guest_ID)).Tables[0];
            Decimal Money = 0;
            Order_Money = dt.Rows[0]["Order_Money"].ToString();
            if (dt != null && dt.Rows.Count > 0)
            {
                IEnumerable<DataRow> enOrder = dt.AsEnumerable();
                var a = (from o in enOrder
                         select new
                         {
                             Type_Name = o.Field<String>("Type_Name"),
                             Type_ID = o.Field<Int32>("Type_ID")
                         }).Distinct().ToList();
                foreach (var b in a)
                {
                    sb.AppendFormat("<h4 class=\"text-success\">{0}</h4>", b.Type_Name);
                    var c = (from o in enOrder
                             where o.Field<Int32>("Type_ID") == b.Type_ID
                             select o).ToList();
                    Money = 0;
                    sb.Append("<table class=\"table table-bordered table-condensed table-hover\">");
                    sb.Append("<thead>");
                    sb.Append("<tr><th>产品编码</th><th>产品名称</th><th>产品价格</th><th>数量</th><th>总金额</th></tr>");
                    sb.Append("</thead>");
                    sb.Append("<tbody>");
                    foreach (var d in c)
                    {
                        Money += Convert.ToDecimal(d["OrderProduct_Money"]);
                        sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>", d["Product_Code"], d["Product_Name"], d["Product_Price"], d["OrderProduct_Number"], d["OrderProduct_Money"]);
                    }
                    sb.Append("</tbody>");
                    sb.Append("<tfoot>");
                    sb.Append("<tr><th class=\"total-label\" colspan=\"4\" style=\"text-align: right;\">小计</th><th class=\"total-amount\">￥： <strong class=\"text-error\">" + Money + "</strong></th></tr>");
                    sb.Append("</tfoot>");
                    sb.Append("</table>");
                }
                //sb.Append("<h4 class=\"text-success\">测试</h4>");

            }
            orderDetailHtml = sb.ToString();
        }
    }
}